/*
 * FunctionDhtmlxTreeMng.java
 *
 * Created on 2007年7月17日, 上午10:36
 *
 * 花开堪折须直折，莫待无花空折枝！
 *
 */

package com.ecomm.util;

import com.ecomm.chat.pojo.ChatUserInfo;
import com.pphome.orgmodel.manager.OrgManager;
import com.pphome.orgmodel.manager.UserManager;
import com.pphome.orgmodel.pojo.OrgInfo;
import com.pphome.orgmodel.pojo.UserInfo;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;

/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>ppliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @data  2007年7月17日, 上午10:36
 */
public class FunctionDhtmlxTreeMng {
    private static Log log = Logger.getLog(FunctionDhtmlxTreeMng.class);
    /** Creates a new instance of FunctionDhtmlxTreeMng */
    public FunctionDhtmlxTreeMng() {
    }
    public String dumpSelectUsere(String id,String userids) throws Exception{
        StringBuffer sRet = new StringBuffer();
        String sSQL = null;
        String name = null;
        sRet.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        sRet.append("<tree  id='"+id+"' >\n");
        List orglist = OrgManager.getInstance().getOrgListByParentid(id);
        for (Iterator it = orglist.iterator(); it.hasNext();) {
            OrgInfo orgInfo = (OrgInfo) it.next();
            List userList = UserManager.getInstance().getUserListByOrgid(orgInfo.getId());
            if (userList!= null && userList.size() >0)      //如果是叶子节点并且组织下面有用户
            {
                sRet.append("<item child='1' id='"+orgInfo.getId()+"' text='" + orgInfo.getName() + "'  ><userdata name='isuser'>false</userdata></item>\n");
            } else {
                sRet.append("<item child='0' id='"+orgInfo.getId()+"' text='" + orgInfo.getName() + "' ><userdata name='isuser'>false</userdata></item>\n");
            }
        }
        //取出当前orgid目录下的所有用户/
        List userList = UserManager.getInstance().getUserListByOrgid(id);
        if (userList.size()!=0) {
            for (Iterator it = userList.iterator(); it.hasNext();) {
                UserInfo userInfo = (UserInfo) it.next();
                sRet.append("<item id='"+userInfo.getId()+"' text='" + userInfo.getName() + "' "+this.getBooleanChecked(userInfo.getId(),userids)+"  ><userdata name='isuser'>true</userdata></item>\n");
            }
        }
        sRet.append("</tree>\n");
        return sRet.toString();
    }
    public String  getBooleanChecked(String userid,String userids){
        if(userids.indexOf(userid) >= 0){
            return " checked='true' ";
        }else{
            return " ";
        }
    }
    
    /**
     * 展开用户树，
     * chat im tree
     * @param id
     *
     * @return
     * @throws OrgModelOperationException
     */
    public String dumpSelectUsere(String id) throws Exception{
        StringBuffer sRet = new StringBuffer();
        String sSQL = null;
        String name = null;
        sRet.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        sRet.append("<tree  id='"+id+"' >\n");
        List orglist = OrgManager.getInstance().getOrgListByParentid(id);
        for (Iterator it = orglist.iterator(); it.hasNext();) {
            OrgInfo orgInfo = (OrgInfo) it.next();
            List userList = UserManager.getInstance().getUserListByOrgid(orgInfo.getId());
            if (userList!= null && userList.size() >0)      //如果是叶子节点并且组织下面有用户
            {
                sRet.append("<item child='1' id='"+orgInfo.getId()+"' text='" + orgInfo.getName() + "'   im1='folderopen.gif' im2='folder.gif'  ><userdata name='isuser'>false</userdata></item>\n");
            } else {
                sRet.append("<item child='0' id='"+orgInfo.getId()+"' text='" + orgInfo.getName() + "'  im1='folderopen.gif' im2='folder.gif'  ><userdata name='isuser'>false</userdata></item>\n");
            }
        }
        //取出当前orgid目录下的所有用户/
        List userList = UserManager.getInstance().getUserListByOrgid(id);
        if (userList.size()!=0) {
            for (Iterator it = userList.iterator(); it.hasNext();) {
                UserInfo userInfo = (UserInfo) it.next();
                sRet.append("<item id='"+userInfo.getId()+"' text='" + userInfo.getName() + "' im0='"+getchatUserInfo(userInfo.getId())+"' ><userdata name='isuser'>true</userdata></item>\n");
            }
        }
        sRet.append("</tree>\n");
        return sRet.toString();
    }
    /**
     *通过用户ID得到
     *得到用户的的图片路径
     */
    public  String  getchatUserInfo(String createUserid){
        ChatUserInfo chatUserInfo = com.ecomm.chat.manager.ChatUserInfoManager.getInstance().getChatUserInfoByUserid(createUserid);
        if(chatUserInfo != null){
            StringBuilder sb = new StringBuilder();
            sb.append(chatUserInfo.getImgPath());
            sb.append("_");
            sb.append(chatUserInfo.getOnlinestate());
            sb.append(".gif");
            return sb.toString();
        }else{
            return "1_0.gif";
        }
    }
}
